package com.whh.dota2helper;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.whh.dota2helper.model.Hero;
import com.whh.dota2helper.service.HeroService;

public class HeroListActivity extends Activity{
	private GridView gv_heros;
	
	private List<Hero> heros;
	
	private HeroService heroService;
	
	private TextView tv_title;
	
	private PopupWindow popWin;
	
	private TextView tv_int;
	
	private TextView tv_agi;
	
	private TextView tv_str;
	
	private HeroListAdapter heroListAdapter;
	
	private static int group = Constant.GROUP_RADIANT;
	private static int type = Constant.HERO_TYPE_INT;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_hero_list);
		heroService = new HeroService(HeroListActivity.this);
		init();
		initListener();
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		//TODO 添加退出功能，修复进入该页面时默认选中哪个阵营和类型BUG
		
	}
	
	private void init(){
		tv_title = (TextView)findViewById(R.id.tv_title);
		tv_title.setText("天  辉");
		tv_title.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.arrow_down , 0);
		tv_title.setCompoundDrawablePadding(10);
		
		tv_int = (TextView)findViewById(R.id.tv_int);
		tv_agi = (TextView)findViewById(R.id.tv_agi);
		tv_str = (TextView)findViewById(R.id.tv_str);
		
		heros = heroService.getAll(group, type);
		gv_heros = (GridView)findViewById(R.id.gv_heros);
		heroListAdapter = new HeroListAdapter();
		gv_heros.setAdapter(heroListAdapter);
		
	}
	
	public void initListener(){
		//头像点击
		gv_heros.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				Intent intent = new Intent(HeroListActivity.this,HeroActivity.class);
				intent.putExtra("id", heros.get(position).getId());
				startActivity(intent);
			}
		});
		
		//头部阵营点击
		tv_title.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				showPopWin();
			}
		});
		
		//英雄类型点击
		tv_int.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				tv_int.setBackgroundColor(getResources().getColor(R.color.color_232425));
				tv_agi.setBackgroundColor(getResources().getColor(R.color.color_00000000));
				tv_str.setBackgroundColor(getResources().getColor(R.color.color_00000000));
				if(type!=Constant.HERO_TYPE_INT){
					type = Constant.HERO_TYPE_INT;
					heros = heroService.getAll(group, type);
					heroListAdapter.notifyDataSetChanged();
				}
			}
		});
		
		tv_agi.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				tv_agi.setBackgroundColor(getResources().getColor(R.color.color_232425));
				tv_int.setBackgroundColor(getResources().getColor(R.color.color_00000000));
				tv_str.setBackgroundColor(getResources().getColor(R.color.color_00000000));
				if(type!=Constant.HERO_TYPE_AGI){
					type = Constant.HERO_TYPE_AGI;
					heros = heroService.getAll(group, type);
					heroListAdapter.notifyDataSetChanged();
				}
			}
		});
		
		tv_str.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				tv_str.setBackgroundColor(getResources().getColor(R.color.color_232425));
				tv_agi.setBackgroundColor(getResources().getColor(R.color.color_00000000));
				tv_int.setBackgroundColor(getResources().getColor(R.color.color_00000000));
				if(type!=Constant.HERO_TYPE_STR){
					type = Constant.HERO_TYPE_STR;
					heros = heroService.getAll(group, type);
					heroListAdapter.notifyDataSetChanged();
				}
			}
		});
	}
	
	private class HeroListAdapter extends BaseAdapter{

		@Override
		public int getCount() {
			return heros.size();
		}

		@Override
		public Object getItem(int position) {
			return heros.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View view, ViewGroup parent) {
			view = LayoutInflater.from(getApplicationContext()).inflate(R.layout.list_item_hero, null);
			final Hero hero = heros.get(position);
			TextView tv_name = (TextView)view.findViewById(R.id.tv_name);
			ImageView iv_header = (ImageView)view.findViewById(R.id.iv_header);
			int headerId = view.getResources().getIdentifier(hero.getHeaderVert(),"drawable", getPackageName());
			tv_name.setText(hero.getNameCn().split("\\|")[0]);
			iv_header.setImageResource(headerId);
			return view;
		}
	}
	
	public void showPopWin(){
		if (popWin != null && popWin.isShowing()) {
			popWin.dismiss();
		} else {
			View popWinView = getLayoutInflater().inflate(R.layout.header_popwin, null);
			ListView listView = (ListView) popWinView.findViewById(R.id.lv_header_popwin);
			PopWinAdapter adapter = new PopWinAdapter(HeroListActivity.this);
			listView.setAdapter(adapter);
			listView.setItemsCanFocus(true);
			listView.setOnItemClickListener(new OnItemClickListener() {
				@Override
				public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
					popWin.dismiss();
					if(getPopWinTitles()[position].equals("天  辉")){
						if(group!=Constant.GROUP_RADIANT){
							tv_title.setText("天  辉");
							group = Constant.GROUP_RADIANT;
							heros = heroService.getAll(group, type);
							heroListAdapter.notifyDataSetChanged();
						}
					}else if(getPopWinTitles()[position].equals("夜  魇")){
						if(group!=Constant.GROUP_DIRE){
							tv_title.setText("夜  魇");
							group = Constant.GROUP_DIRE;
							heros = heroService.getAll(group, type);
							heroListAdapter.notifyDataSetChanged();
						}
					}
				}
			});
			popWin = new PopupWindow(popWinView, LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
			// 点击其他地方隐藏弹出框
			popWin.getContentView().setOnTouchListener(
				new OnTouchListener() {
						@Override
						public boolean onTouch(View v, MotionEvent event) {
							popWin.dismiss();
							return true;
						}
				});
			popWin.setFocusable(true);
			popWin.update();
			int x = (listView.getLayoutParams().width-tv_title.getWidth())/2;
			popWin.showAsDropDown(findViewById(R.id.tv_title),-x, 5);
		}
	}
	
	class PopWinAdapter extends BaseAdapter {
		private LayoutInflater inflater;

		public PopWinAdapter(Context context) {
			inflater = LayoutInflater.from(context);
		}

		@Override
		public int getCount() {
			return getPopWinTitles().length;
		}

		@Override
		public Object getItem(int position) {
			return getPopWinTitles()[position];
		}

		@Override
		public long getItemId(int position) {
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			final String t = getPopWinTitles()[position];
			convertView = inflater.inflate(R.layout.header_popwin_item,null);
			TextView title = (TextView) convertView.findViewById(R.id.tv_header_popwin_item);
			title.setText(t);
			return convertView;
		}
	}
	
	private String[] getPopWinTitles(){
		return new String[]{"天  辉","夜  魇"};
	}
}
